package com.wsc.wsckeeper.components;

import java.util.Random;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wsckeeper.R;

public class SpinerPicker extends FrameLayout {
	private static final int DEF_MAX_VALUE = 20;
	private static final int DEF_AMOUNT_VALUE = 2;
	private ImageView imgLeft;
	private ImageView imgRight;
	private EditText etValue;
	private TextView tvSymbols;

	private String vSymbols;
	private int vAmount;

	private Random random = new Random();
	private SCallback callback;

	private StringBuilder values;

	public SpinerPicker(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initComponent();
		initAttrs(context, attrs);
		initValue();
		initListeners();
	}

	public SpinerPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		initComponent();
		initAttrs(context, attrs);
		initValue();
		initListeners();
	}

	public SpinerPicker(Context context) {
		super(context);
		initComponent();
		initValue();
		initListeners();
	}

	private void initComponent() {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.spiner_picker, this);

		imgLeft = (ImageView) findViewById(R.id.arrow_left);
		imgRight = (ImageView) findViewById(R.id.arrow_right);
		etValue = (EditText) findViewById(R.id.value);
		tvSymbols = (TextView) findViewById(R.id.symbols);
	}

	public void initAttrs(Context context, AttributeSet attrs) {
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.spiner_picker, 0, 0);

		try {
			vSymbols = a.getString(R.styleable.spiner_picker_symbols);
			vAmount = a.getInt(R.styleable.spiner_picker_amount, DEF_AMOUNT_VALUE);
		} finally {
			a.recycle();
		}
	}

	private void initValue() {
		tvSymbols.setText(vSymbols);
		etValue.setText(String.valueOf(vAmount));
	}

	private void initListeners() {
		imgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tryChangeValue(-1);
			}
		});

		imgRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tryChangeValue(1);
			}
		});
	}

	public String getValue() {
		values = new StringBuilder();
		int lenght = Integer.parseInt(etValue.getText().toString());

		for (int i = 0; i < lenght; i++) {
			int index = random.nextInt(vSymbols.length());
			values.append(vSymbols.charAt(index));
		}

		return values.toString();
	}

	private void tryChangeValue(int change) {
		int value = Integer.parseInt(this.etValue.getText().toString()) + change;

		if (value > -1 && value < DEF_MAX_VALUE + 1) {
			this.etValue.setText(String.valueOf(value));
			callback.run(null);
		}
	}

	public void addCallback(SCallback callback) {
		this.callback = callback;
	}
}
